<!--ID:agent-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link href="../../css/mine-agent.css" rel="stylesheet"/>
</head>

<body>
<div class="head">
    <img class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>我的代理商进度</p>
</div>
<!--<div class="bar-content">
    <p class="te-center"><img id="state-img" class="te-center" src="../../images/yellow-gou.png" /></p>
    <p class="te-center" id="agent-state">申请成功！</p>
    <p class="rank">我是<span></span></p>
    <p class="apply-time">申请时间：
        <span></span></p>
    <p class="bank-type">银行
        <span class="bank-r">fgre </span></p>
</div>-->
<hr>
<div class="content" id="content">
    <!--<div class="agent-state">
        <div class="">
            <label for="type">业务类别 :</label>
            <span name="type">信用卡</span>
        </div>
        <div class="">

            <label for="apply-time">申请时间 :</label>
            <span name="apply-time" class="time">2017-02-03 13:40</span>
        </div>
        <div class="">
            <label for="money">加盟费金额: </label>
            <span name='momey'>1</span>元
        </div>
        <div>
            <label for="state">剩余次数 : </label>
            <span name="state">1</span>次
        </div>
    </div>-->
    <!--<div class="agent-state">
        <div class="item-le">
            <p class="type">信用卡</p>
            <p class="item-time">2017-02-03 13:40</p>
        </div>
        <div class="item-ri">
            <p class='momey'>剩余 <span class="number-c">2</span> 次</p>
            <p class="number-rank">代理等级 <span class="number-c">1</span></p>
        </div>
    </div>
    -->
</div>
<p class="add-img">
    <!--<i class="icon-add"></i>-->
</p>

<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../libs/mui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/url.js" type="text/javascript" charset="utf-8"></script>
<script>
    $('.head img').removeClass('mui-action-back');
    $('.head img').on('tap', function () {
        window.location.href = '../head-foot-demo.html#/mine';
    })

    //代理商进度接口
    $.ajax({
        type: "get",
        url: ur + "rest/partnerController/getPartnerState",
        async: true,
        headers: {
            'X-Token': localStorage.token
        },
        dataType: 'json',
        success: function (data) {
            if (data.meta.code === 'S002') {
                var barContent = '<div class="bar-content"><p class="te-center">' +
                    '<img id="state-img" class="te-center" src="../../images/yellow-gou.png" /></p>' +
                    '<p class="te-center" id="agent-state"></p><p class="rank">我是<span></span></p>' +
                    '<p class="apply-time">申请时间：<span></span></p><p class="bank-type">银行<span class="bank-r">' +
                    data.data.bank + '</span></p></div>';
                $('.head').after(barContent);
                //代理商身份
                if (data.data.type == 1) {
                    $('.rank').find('span').text('贷款中介');
                    $('.bank-type').remove()
                } else {
                    $('.rank').find('span').text('银行经理')
                }
                //申请时间
                $('.apply-time').find('span').text(new Date(data.data.date).toLocaleString())
                if (data.data.state == 1) {
                    $('#agent-state').text('等待审核');
                    $('#state-img').attr('src', '../../images/mine-img9.png')

                } else if (data.data.state == 2) {
                    $('#agent-state').text('申请成功！');
                    $('.add-img').append('<i class="icon-add"></i>');
                    $('.add-img').on('tap', '.icon-add', function () {
                        href("select-work.html", "select-work.html")
                    });
                    business();
                    //状态图片
                    $('#state-img').attr('src', '../../images/yellow-gou.png')
                } else {
                    $('#agent-state').text('申请失败！');
                    $('#state-img').attr('src', '../../images/mine-img9.png')
                    //修改资料
                    $('.bar-content').append('<button id="re-alter">重新修改资料</button>');
                    $('body').on('tap', '#re-alter', function () {
                        href('../join/fill-in.html', 'fill-in.html')
                    })
                }
            } else {
                mui.toast(data.meta.message);
            }
        }
    });
    Date.prototype.toLocaleString = function () {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + ' ' + this.getHours() + ":" + this.getMinutes();
    };
    //代理商业务接口
    function business(pay) {
        var rank = ['', '铜', '银', '金'];
        $.ajax({
            type: "GET",
            url: ur + "rest/loanPartnerController/applicationList",
            async: true,
            dataType: 'json',
            headers: {
                'X-Token': localStorage.token,
            },
            success: function (data) {
                if (data.meta.code === "S002") {
                    var applyData = data.data.list;
                    var itemL = $('.agent-state').length;

                    //次数更新
                    if (pay) {
                        for (var j = 0; j < $('.state').length; j++) {
                            if (applyData[$('.state').length - 1 - j].number != parseInt($('.state').eq(j).text())) {
                                $('.state').eq(j).text(applyData[$('.state').length - 1 - j].number)
                            }
                        }
                    } else {
                        //业务条数添加
                        for (var i = applyData.length - 1; i >= itemL; i--) {
                            var addimg = applyData[i].state != 1 ? '<img src = "../../images/ri-fail.png" />' : '';
                            var appendApply = '<div class="agent-state" type="' +
                                applyData[i].loanCode + '" ids="' +
                                applyData[i].id + '"><div class="item-le"><p class="type">' +
                                applyData[i].loanName + '</p><p class="item-time">' +
                                new Date(applyData[i].date).toLocaleString() + '</p></div><div class="item-ri"><p class="momey">剩余 <span class="number-c cis">' +
                                applyData[i].number + '</span> 次</p><p class="number-rank">代理等级 <span class="number-c">' +
                                rank[applyData[i].level] + '</span></p></div></div>'
                            $('.content').append(appendApply);
                        }
                    }
                } else {
                    mui.toast(data.meta.message);
                }
            }
        });
    }
    //添加AddNew自定义事件监听 刷新业务列表增加条数
    window.addEventListener('agents', function () {
        business()
    });
    //添加AddNew自定义事件监听 刷新业务列表 支付后更新次数
    window.addEventListener('pay', function () {
        business('pay');
    });
    $('#content').on('tap', '.agent-state', function () {
        if ($(this).find('.cis').text() == 0) {
            localStorage.surehow = 1;
            localStorage.sureallprice = $(this).find('.momey').text();
            localStorage.carid = $(this).attr('ids');
            localStorage.loanCode = $(this).attr('type');
            href('select-rank.html');
        } else {
            mui.toast('您已完成付款！');
        }
    })
</script>
</body>

</html>